package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/23.
 */
@Component
public interface QueryDistributorDAO {
    @Select("select  (CASE WHEN t.task_type = '0' THEN '分销商' WHEN task_type = '1' THEN ' 供应商' END)task_type,t.task_id,t.user_id,DATE_FORMAT(t.create_time,'%Y-%m-%d %T')create_time,seller_name,t.user_name,i.nick_name,phone,s.* from infu_user_request_type_task t LEFT JOIN seller_shopinfo s on t.user_id=s.ru_id and s.audit_status=0 and shop_type=#{taskType} LEFT JOIN infs_user i on i.user_id=t.user_id where t.task_type=#{taskType} and t.status=#{status}")
    List<Map<String,Object>>queryAllDistributor(Pagination page,@Param("taskType") String taskType, @Param("status") String status);

    @Select("select  (CASE WHEN task_type = '0' THEN '分销商' WHEN task_type = '1' THEN ' 供应商' END)task_type,task_id,user_id,admin_id,DATE_FORMAT(create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(check_time,'%Y-%m-%d %T')check_time,check_remark from infu_user_request_type_task where task_type=#{taskType} and status=#{status}")
    List<Map<String,Object>>queryPassAndUnPasssDistributor(Pagination page,@Param("taskType") String taskType, @Param("status") String status);

    @Select("select count(*) mount from infu_user_request_type_task where task_type=#{taskType} and status=0 union all select count(*) from infu_user_request_type_task where task_type=#{taskType} and status=1 union all select count(*) from infu_user_request_type_task where task_type=#{taskType} and status=2")
    List<Map<String,Object>>queryDistributorAmount(@Param("taskType")String taskType);
//    @Select("select * from seller_shopinfo where ru_id=#{ruId} and audit_status=#{AuditStatus}")
//    List<Map<String,Object>>querySellerShopinfo(@Param("ruId")Integer ruId,@Param("AuditStatus") Integer AuditStatus);

    @Update("update seller_shopinfo set audit_status=#{auditStatus} where ru_id=${userId}")
    int updateSellerShopinfo(@Param("auditStatus")Integer auditStatus,@Param("userId")Integer userId);

    @Update("update infu_user_request_type_task set status=#{status},admin_id=#{adminId},check_time=now(),check_remark=#{checkRemark} where task_id=#{taskId}")
    int updateDistributor(@Param("status") Integer status, @Param("adminId") Integer adminID, @Param("checkRemark") String checkRemark, @Param("taskId") Integer taskId);

    @Update("update infu_user_request_type_task set status=#{status},check_remark=#{checkRemark},user_name=#{userName},phone=#{phone},seller_name=#{sellerName} where task_id=#{taskId}")
    int newUpdateDistributor(@Param("status") Integer status, @Param("checkRemark") String checkRemark ,@Param("userName") String userName, @Param("phone") String phone,@Param("sellerName") String sellerName,@Param("taskId") Integer taskId);

    /**分销商审核通过生成店铺
     * @param userId
     * @param shopType
     * @param sellerName
     * @param shopTitle
     * @param shopKeyword
     * @param country
     * @param province
     * @param city
     * @param district
     * @param shopAddress
     * @param mobile
     * @param status
     * @param auditStatus
     * @param sellerShopInfoMap
     * @return
     */
    @Insert("insert into seller_shopinfo (ru_id,shop_type,shop_name,shop_title,shop_keyword,country,province,city,district,shop_address,mobile,status,audit_status) value (#{userId},#{shopType},#{shopName},#{shopTitle},#{shopKeyword},#{country},#{province},#{city},#{district},#{shopAddress},#{mobile},#{status},#{auditStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "sellerShopInfoMap.id",keyColumn = "id")
    int insertInfoDistributor(@Param("userId") Integer userId,@Param("shopType") Integer shopType,@Param("shopName") String sellerName,@Param("shopTitle") String shopTitle,@Param("shopKeyword") String shopKeyword,@Param("country") Integer country,@Param("province") Integer province ,@Param("city") Integer city,@Param("district") Integer district,@Param("shopAddress") String shopAddress,@Param("mobile") String mobile,@Param("status") Integer status,@Param("auditStatus") Integer auditStatus,@Param("sellerShopInfoMap") Map sellerShopInfoMap);

    /**给商家店铺配置默认配送模板
     * @param shippingTempletMap
     * @return
     */
    @Insert("insert into shipping_templet(seller_shopinfo_id,shipping_templet_name,shipping_templet_remark,shipping_templet_status,shipping_title,templet_type) VALUES(#{sellerShopinfoId},#{shippingTempletName},#{shippingTempletRemark},#{shippingTempletStatus},#{shippingTitle},#{templetType})")
    int insertShippingTemplet(Map shippingTempletMap);


    @Update("update INFS_USER set user_type=1 where user_id=#{userId}")
    int updateInfoUser(@Param("userId") Integer userId);

    @Select("select * from infs_user where user_id=#{userId}")
    Map<String,String> queryInfsUserById(@Param("userId") Integer userId);

    @Insert("insert into INFS_ADMIN_USER (user_id,user_name,email,password,add_time,role_id)values(#{userId},#{userName},#{email},#{password},now(),#{roleId})")
    int insertInfsAdminUser(@Param("userId")Integer userId,@Param("userName") String userName, @Param("email") String email, @Param("password") String password, @Param("roleId") Integer roleId);
}
